# This file has been taken from libroxml and modfied to suit the needs
# of libroxsd. Both files share the same structure.

# O (Output) is the build directory
ifeq '$O' ''
O = .
endif
# V (Verbosity) is 0 (quiet) or 1 (verbose)
ifeq '$V' ''
V = 0
endif
# D (Debug) is 0 (optimize) or 1 (build debug version)
ifeq '$D' ''
D = 0
endif

# files
INC = inc/roxsd.h
SRC_LIB = \
	src/roxsd-api.c \
	src/roxsd-list.c \
	src/roxsd-error.c \
	src/roxsd-compiler.c \
	src/roxsd-compiler-schema.c \
	src/roxsd-compiler-successor.c \
	src/roxsd-compiler-xsdtype.c

gen-compiler-schema = $O/src/roxsd-compiler-schema-gen.inc

SRC_ADD =  \
	$(gen-compiler-schema)

SRC_BIN = src/roxsd-tool.c

DEPS = $(patsubst %.c, $O/%.d, $(SRC_LIB) $(SRC_BIN))
OBJS = $(OBJ_LIB) $(OBJ_BIN)
OBJ_LIB = $(SRC_LIB:%.c=$O/%.o)
OBJ_BIN = $(SRC_BIN:%.c=$O/%.o)
TARGETS = $(TARGET_SLIB) $(TARGET_LN) $(TARGET_LIB) $(TARGET_BIN)
TARGET_SLIB = $O/libroxsd.a
TARGET_LIB = $O/libroxsd.so.0
TARGET_LN = $O/libroxsd.so
TARGET_BIN = $O/roxsd
BINS = $(TARGET_SLIB) $(TARGET_LIB) $(TARGET_LN) $(TARGET_BIN)

SED ?= sed

# specific, modifiable flags
# set D=1 on command line to produce debuggable binary
ifeq ("$(OPTIM)", "")
ifeq '$D' '0'
OPTIM = -O3 -DNDEBUG
else
OPTIM = -g -O0
endif
endif

# options
CFLAGS += -Iinc/ -I$O/src -I$O/inc
CFLAGS += -std=c99 -D_POSIX_C_SOURCE=201204L
CFLAGS += $(OPTIM) -pthread -fPIC -DPIC
CFLAGS += -Wall -Wextra -Wno-unused -Werror
LDFLAGS += -pthread

# first rule (default)
all:

config-info:
	@ echo
	@ echo "======================================================= Machine"
	@ LC_ALL=C uname -a
	@ echo
	@ echo "=================================================== CPP version"
	@ LC_ALL=C $(CPP) -v
	@ echo
	@ echo "==================================================== CC version"
	@ LC_ALL=C $(CC) -v
	@ echo
	@ echo "=================================================== SED version"
	@ LC_ALL=C $(SED) --version
	@ echo
	@ echo "================================================== MAKE version"
	@ LC_ALL=C $(MAKE) -v
	@ echo "==============================================================="
	@ echo

# dependencies
ifeq ($(or \
	$(findstring doxy, $(MAKECMDGOALS)), \
	$(findstring clean, $(MAKECMDGOALS)), \
	$(findstring mrproper, $(MAKECMDGOALS)), \
	$(findstring uninstall, $(MAKECMDGOALS)) \
),)
-include $(DEPS)
endif

# build-related variables
include other.mk

# rules verbosity
ifneq '$V' '0'
P = @ true
E =
else
P = @ echo
E = @
endif

# rules

$O/src :
	$P '  MKDIR   $(@F)'
	$E mkdir -p $@

$O/%.d : %.c | $O/src
	$P '  DEP     $(@F)'
	$E $(CC) -MM -MT '$@ $O/$*.o' $(CFLAGS) -DMAKE_DEPENDS $< -MF $@ || rm -f $@

$O/%.o : %.c
	$P '  CC      $(@F)'
	$E $(CC) -c $(CPPFLAGS) $(CFLAGS) $< -o $@

$(SRC_LIB) $(SRC_BIN): $O/inc/roxsd.h

$O/inc/roxsd.h: $O/inc inc/roxsd.h.in
	$P '  SED     $(@F)'
	$E $(SED) "s:@ROXSD_VERSION@:\"$(ROXSD_VERSION)\":" inc/roxsd.h.in > $@

$O/inc:
	$P '  MKDIR   $(@F)'
	$E mkdir -p $@

$O/src/roxsd-compiler-schema.o: $(gen-compiler-schema)

$(gen-compiler-schema): src/roxsd-compiler-schema.c check-types.sh
	$P '  GEN     $(@F)'
	$E V=$V check-types.sh list && { check-types.sh > $@ ; }

$(TARGET_SLIB) : $(OBJ_LIB)
	$P '  AR      $(@F)'
	$E $(AR) rc $@ $^

$(TARGET_LIB) : $(OBJ_LIB)
	$P '  LD      $(@F)'
	$E $(CC) -shared $(LINKERFLAG) $(LDFLAGS) $^ -o $@

$(TARGET_LN): $(TARGET_LIB)
	$P '  LN      $(notdir $@)'
	$E - ln -fs $(<F) $@

$(TARGET_BIN): $(OBJ_BIN)
$(TARGET_BIN): | $(if $(filter -static, $(LDFLAGS)), $(TARGET_SLIB), $(TARGET_LIB))
	$P '  LD      $(@F)'
	$E $(CC) $(LDFLAGS) $^ -L$O -lroxsd -lroxml -o $@

.PHONY : all
all : $(TARGET_SLIB) $(if $(filter -static, $(LDFLAGS)), , $(TARGET_LN)) $(TARGET_BIN)

.PHONY : doxy
doxy : doxy.cfg man.cfg
	$P '  DOXYGEN'
	$E - doxygen doxy.cfg &>/dev/null
	$P '  MAN'
	$E - doxygen man.cfg &>/dev/null

.PHONY: clean
clean:
ifeq ($(strip $(O)),.)
	$P '  RM      deps objs bins'
	$E rm -f $(DEPS) $(OBJS) $(BINS)
else
	$P '  RM      build directory'
	$E rm -rf $O
endif
	$P '  RM      roxsd.h'
	$E rm -f $O/inc/roxsd.h
	$P '  RM      generated source files'
	$E rm -f $(SRC_ADD)

.PHONY : mrproper
mrproper : clean
	$P '  RM      docs'
	$E rm -fr docs/man docs/html docs/latex

.PHONY: install
install: $(TARGETS) doxy
	$P '  INSTALL DIRS'
	$E mkdir -p $(DESTDIR)/usr/bin
	$E mkdir -p $(DESTDIR)/usr/include
	$E mkdir -p $(DESTDIR)/usr/lib/pkgconfig
	$E mkdir -p $(DESTDIR)/usr/share/man/man3
	$E mkdir -p $(DESTDIR)/usr/share/man/man1
	$E mkdir -p $(DESTDIR)/usr/share/doc/libroxsd/html
	$P '  INSTALL FILES'
	$E install -D $(TARGET_SLIB) $(DESTDIR)/usr/lib
	$E cp -fpR $(TARGET_LN) $(DESTDIR)/usr/lib/libroxsd.so
	$E install -D $(TARGET_LIB) $(DESTDIR)/usr/lib
	$E install -D $(TARGET_BIN) $(DESTDIR)/usr/bin
	$E $(SED) "s:@ROXSD_VERSION@:\"$(ROXSD_VERSION)\":" inc/roxsd.h.in > $(DESTDIR)/usr/include/roxsd.h
	$E install -D docs/roxsd.1 $(DESTDIR)/usr/share/man/man1/
	$E [ ! -d docs/man/man3 ] || install -D docs/man/man3/* $(DESTDIR)/usr/share/man/man3/
	$E [ ! -d docs/html ] || install -D docs/html/* $(DESTDIR)/usr/share/doc/libroxsd/html/
	$E install -m644 libroxsd.pc $(DESTDIR)/usr/lib/pkgconfig
	$E cp -d $(TARGET_LN) $(DESTDIR)/usr/lib

.PHONY: uninstall
uninstall:
	$P '  UNINSTALL'
	$E rm -f $(DESTDIR)/usr/lib/pkgconfig/libroxsd.pc
	$E rm -f $(DESTDIR)/usr/lib/$(TARGET_SLIB)
	$E rm -f $(DESTDIR)/usr/lib/$(TARGET_LIB)
	$E rm -f $(DESTDIR)/usr/bin/$(TARGET_BIN)
	$E rm -f $(DESTDIR)/usr/include/$(INC)
	$E rm -fr $(DESTDIR)/usr/share/doc/libroxsd
	$E rm -fr $(DESTDIR)/usr/share/man/man1/roxsd.1
	$E rm -fr $(DESTDIR)/usr/share/man/man3/roxsd*
	$E rm -fr $(DESTDIR)/usr/share/man/man3/ROXSD*

.PHONY: help
help:
	@echo
	@echo "make [D=1] [V=1] [O=path] [OPTIM=optimize-options]"
	@echo "   D=1: build debug version (default: D=0)"
	@echo "   V=1: verbose output (default: V=0)"
	@echo "   O=path: build binary in path (default: O=.)"
	@echo "   OPTIM=opt: force optimization options (default: OPTIM=-O3 if D=0, OPTIM=\"-g -O0\" if D=1)"
	@echo
	@echo "A few other variables can be of interest to you:"
	@echo "   DESTDIR=directory: the installation directory (useful to simulate the install process)"
	@echo
	@echo "The default options build the binaries in the current directory, optimized for speed (-O3)"
	@echo

